package com.jgc.stage01.wait_notify;

/**
 * Created by Administrator on 2022/2/10.
 */
public class Print {
    private int flag = 1;//信号量。当值为1时打印数字，当值为2时打印字母
    private int count = 1;
    //在调用wait()、notify()之前，要先通过synchronized关键字同步给对象，也就是给该对象加锁。
    public synchronized void printNum(){
        if(flag != 1){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print(2 * count - 1);
        System.out.print(2 * count);
        flag = 2;
        notify();
    }
    public synchronized void printChar(){
        if(flag!=2){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print((char) (count - 1 + 'A'));
        count++;//当一轮循环打印完之后，计数器加1
        flag = 1;
        notify();
    }

}
